home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1320 / 1320.xpi / chrome / gmanager.jar / content / overlay.js < prev    next >
Text File  |  2010-01-22  |  10KB  |  321 lines

  1. // Gmail Manager
  2. // By Todd Long <longfocus@gmail.com>
  3. // http://www.longfocus.com/firefox/gmanager/
  4.  
  5. function gmanager_OverlayLoad(aEvent)
  6. {
  7.   // Load the overlay
  8.   gmanager_Overlay.load();
  9. }
  10.  
  11. function gmanager_OverlayUnload(aEvent)
  12. {
  13.   // Unload the overlay
  14.   gmanager_Overlay.unload();
  15. }
  16.  
  17. function gmanager_ContentAreaClick(aEvent)
  18. {
  19.   // Load the accounts manager
  20.   var manager = Components.classes["@longfocus.com/gmanager/manager;1"].getService(Components.interfaces.gmIManager);
  21.   var global = manager.global;
  22.   var href = gmanager_Utils.getHref(aEvent.target);
  23.   
  24.   switch (aEvent.button)
  25.   {
  26.     case 0: // Left Click
  27.     case 1: // Middle Click
  28.       var isCompose = global.getBoolPref("compose-mailto-links");
  29.       
  30.       if (isCompose && gmanager_Utils.isMailtoLink(href))
  31.       {
  32.         var email = global.getCharPref("compose-mailto-default");
  33.         var location = global.getCharPref("compose-tab-location");
  34.         
  35.         gmanager_Accounts.loadCompose(email, location, href);
  36.         
  37.         // Prevent the default mail client from loading
  38.         aEvent.preventDefault();
  39.       }
  40.       break;
  41.     case 2: // Right Click
  42.     {
  43.       var isHidden = global.getBoolPref("general-hide-context-menu");
  44.       
  45.       if (!isHidden && !gmanager_Utils.isMailtoLink(href))
  46.         isHidden = global.getBoolPref("compose-context-menu");
  47.       
  48.       document.getElementById("gmanager-context-menu-separator").hidden = isHidden;
  49.       document.getElementById("gmanager-context-menu").hidden = isHidden;
  50.       
  51.       break;
  52.     }
  53.     default:
  54.       break;
  55.   }
  56. }
  57.  
  58. var gmanager_Overlay = new function()
  59. {
  60.   this.load = function()
  61.   {
  62.     // Load the logger service
  63.     this._logger = Components.classes["@longfocus.com/gmanager/logger;1"].getService(Components.interfaces.gmILogger);
  64.     
  65.     // Load the accounts manager
  66.     this._manager = Components.classes["@longfocus.com/gmanager/manager;1"].getService(Components.interfaces.gmIManager);
  67.     
  68.     // Load the observers
  69.     var observer = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  70.     observer.addObserver(this, gmanager_Prefs.NOTIFY_CHANGED, false);
  71.     observer.addObserver(this, gmanager_Accounts.NOTIFY_STATE, false);
  72.     
  73.     // Check for new version
  74.     if (this._manager.version != gmanager_Prefs.getCharPref("version"))
  75.       this._welcome();
  76.     
  77.     // Load the mail accounts
  78.     this._loadAccounts(true);
  79.     
  80.     // Toggle the Tools Menu
  81.     this._toggleToolsMenu();
  82.   }
  83.   
  84.   this.unload = function()
  85.   {
  86.     var observer = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  87.     try {
  88.       // Remove the observers
  89.       observer.removeObserver(this, gmanager_Prefs.NOTIFY_CHANGED);
  90.       observer.removeObserver(this, gmanager_Accounts.NOTIFY_STATE);
  91.     } catch(e) {
  92.       this._logger.log("Error removing the observers!");
  93.     }
  94.   }
  95.   
  96.   this._welcome = function()
  97.   {
  98.     // Get the previous version
  99.     var version = gmanager_Prefs.getCharPref("version");
  100.     
  101.     this._logger.log("Previous version = " + version);
  102.     
  103.     if (version < "0.5")
  104.     {
  105.       var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  106.       var prefBranch = prefService.getBranch("gmanager.");
  107.       
  108.       if (prefBranch.prefHasUserValue("accounts"))
  109.       {
  110.         var accounts = prefBranch.getCharPref("accounts").split(",");
  111.         
  112.         for (var i = 0; i < accounts.length; i++)
  113.         {
  114.           var email = accounts[i] + "@gmail.com";
  115.           
  116.           if (!this._manager.isAccount(email))
  117.           {
  118.             var password = gmanager_Utils.getStoredPassword("gmanager.gmail.account", accounts[i]);
  119.             
  120.             this._logger.log("Migrating email: " + email);
  121.             
  122.             this._manager.addAccount("gmail", email, email, password, null);
  123.           }
  124.         }
  125.         
  126.         // Save the accounts
  127.         this._manager.save();
  128.       }
  129.       
  130.       if (prefBranch.prefHasUserValue("current"))
  131.       {
  132.         var current = prefBranch.getCharPref("current").split(",")[0];
  133.         gmanager_Prefs.setCharPref("current", current);
  134.       }
  135.     }
  136.     
  137.     if (version < "0.6")
  138.     {
  139.       if (gmanager_Prefs.hasPref("current"))
  140.       {
  141.         var current = gmanager_Prefs.getCharPref("current");
  142.         var account = this._manager.getAccount(current);
  143.         
  144.         // Set the account to be displayed
  145.         account.setBoolPref("toolbar-display", true);
  146.         
  147.         // Save the accounts
  148.         this._manager.save();
  149.       }
  150.     }
  151.     
  152.     // Check if this is the first time the extension has run
  153.     if (gmanager_Prefs.getBoolPref("first-time"))
  154.     {
  155.       var passwords = gmanager_Utils.getStoredPasswords();
  156.       
  157.       // Open the migrate window if any passwords exist
  158.       if (passwords && passwords.length > 0)
  159.         window.openDialog("chrome://gmanager/content/migrate/migrate.xul", "migrate", "centerscreen,chrome,resizable=yes", passwords);
  160.       
  161.       // Mark the extension as having already run
  162.       gmanager_Prefs.setBoolPref("first-time", false);
  163.     }
  164.     
  165.     this._logger.log("Current version = " + this._manager.version);
  166.     
  167.     // Set the current version
  168.     gmanager_Prefs.setCharPref("version", this._manager.version);
  169.   }
  170.   
  171.   this._loadAccounts = function(aInit)
  172.   {
  173.     var accounts = this._manager.getAccounts({});
  174.     var isAutoLogin = this._manager.global.getBoolPref("general-auto-login");
  175.     var theToolbarPanel = document.getElementById("gmanager-toolbar-panel");
  176.     var activeToolbarPanels = 0;
  177.     
  178.     for (var i = 0; i < accounts.length; i++)
  179.     {
  180.       var account = accounts[i];
  181.       var toolbarPanel = document.getElementById("gmanager-toolbar-panel-" + account.email);
  182.       
  183.       // Check if the toolbar panel exists
  184.       if (toolbarPanel)
  185.       {
  186.         toolbarPanel.updateDisplay();
  187.         toolbarPanel.updatePosition();
  188.       }
  189.       else
  190.       {
  191.         toolbarPanel = theToolbarPanel.cloneNode(true);
  192.         toolbarPanel.account = account;
  193.         toolbarPanel.displayAccount = account;
  194.       }
  195.       
  196.       if (!toolbarPanel.hidden)
  197.         activeToolbarPanels++;
  198.       
  199.       // Check if the account should automatically login
  200.       if (aInit && !account.loggedIn && (isAutoLogin || account.getBoolPref("general-auto-login")))
  201.         account.login(null);
  202.     }
  203.     
  204.     if (gmanager_Prefs.hasPref("current"))
  205.     {
  206.       var current = gmanager_Prefs.getCharPref("current");
  207.       
  208.       if (this._manager.isAccount(current))
  209.         theToolbarPanel.displayAccount = this._manager.getAccount(current);
  210.     }
  211.     
  212.     // Display the main toolbar panel if no account toolbar panels are displayed
  213.     theToolbarPanel.hidden = (activeToolbarPanels > 0);
  214.     
  215.     var toolbarPanels = gmanager_Utils.getAccountToolbars();
  216.     
  217.     for (var i = 0; i < toolbarPanels.length; i++)
  218.     {
  219.       var toolbarPanel = toolbarPanels[i];
  220.       
  221.       if (toolbarPanel.account)
  222.       {
  223.         var email = toolbarPanel.account.email;
  224.         
  225.         if (!this._manager.isAccount(email))
  226.           toolbarPanel.destroy();
  227.       }
  228.     }
  229.   }
  230.   
  231.   this._toggleToolsMenu = function()
  232.   {
  233.     // Display the Tools Menu
  234.     var toolsMenu = document.getElementById("gmanager-tools-menu");
  235.     if (toolsMenu)
  236.       toolsMenu.collapsed = this._manager.global.getBoolPref("general-hide-tools-menu");
  237.   }
  238.   
  239.   this.observe = function(aSubject, aTopic, aData)
  240.   {
  241.     this._logger.log("aSubject = " + aSubject);
  242.     this._logger.log("aTopic = " + aTopic);
  243.     this._logger.log("aData = " + aData);
  244.     
  245.     if (aTopic == gmanager_Prefs.NOTIFY_CHANGED)
  246.     {
  247.       // aSubject : null
  248.       // aTopic   : gmanager_Prefs.NOTIFY_CHANGED
  249.       // aData    : null
  250.       
  251.       // Load the mail accounts
  252.       this._loadAccounts(false);
  253.       
  254.       // Toggle the Tools Menu
  255.       this._toggleToolsMenu();
  256.     }
  257.     else if (aTopic == gmanager_Accounts.NOTIFY_STATE)
  258.     {
  259.       // aSubject : null
  260.       // aTopic   : gmanager_Accounts.NOTIFY_STATE
  261.       // aData    : email|status (e.g. longfocus@gmail.com|10)
  262.       
  263.       var email = (aData ? aData.split("|")[0] : null);
  264.       var status = parseInt(aData ? aData.split("|")[1] : null);
  265.       var account = this._manager.getAccount(email);
  266.       
  267.       this._logger.log("email = " + email);
  268.       this._logger.log("status = " + status);
  269.       
  270.       // Check if the account exists
  271.       if (account)
  272.       {
  273.         switch (status)
  274.         {
  275.           case Components.interfaces.gmIService.STATE_CONNECTING:
  276.           case Components.interfaces.gmIService.STATE_LOGGED_OUT:
  277.             break;
  278.           case Components.interfaces.gmIService.STATE_LOGGED_IN:
  279.           {
  280.             if (account.newMail)
  281.             {
  282.               // Play sound
  283.               if (account.getBoolPref("notifications-sounds"))
  284.               {
  285.                 var file = account.getCharPref("notifications-sounds-file");
  286.                 this._logger.log("Playing sound file: " + file);
  287.                 gmanager_Sounds.play(file);
  288.               }
  289.               
  290.               // Show snippets
  291.               if (account.getBoolPref("notifications-display-snippets"))
  292.               {
  293.                 this._logger.log("Displaying alerts for: " + email);
  294.                 gmanager_Alerts.display(email);
  295.               }
  296.             }
  297.             
  298.             break;
  299.           }
  300.           case Components.interfaces.gmIService.STATE_ERROR_PASSWORD:
  301.           {
  302.             var loginWindowName = "gmanager-login-" + account.email;
  303.             
  304.             if (!gmanager_Utils.isWindow(loginWindowName))
  305.               window.openDialog("chrome://gmanager/content/login/login.xul", loginWindowName, "centerscreen,chrome,dependent=no", account);
  306.             
  307.             break;
  308.           }
  309.           case Components.interfaces.gmIService.STATE_ERROR_NETWORK:
  310.           case Components.interfaces.gmIService.STATE_ERROR_TIMEOUT:
  311.             break;
  312.           default:
  313.           {
  314.             this._logger.log("Unknown state...definitely should not be here!");
  315.             break;
  316.           }
  317.         }
  318.       }
  319.     }
  320.   }
  321. }